package com.my.sort;

import java.util.Arrays;

public class KindsOfSort {

	public static void main(String[] args) {
		int[] arr = {3, 6, 1, 2, 8, 5, 0, 7, 9};
		
		Object[] arrs = new Object[10];
		
		//selectSort2(arr);
		
		int[] arr2 = {3, 6, 1, 2, 8, 5, 0, 7, 9};
		//bubbleSort(arr2);
		
		int[] arr3 = {3, 6, 1, 2, 8, 5, 0, 7, 9};
		insertSort(arr3);
		
		int[] arr4 = {3, 6, 1, 2, 8, 5, 0, 7, 9};
		//threadSort(arr4);
		
		find(arr3, 7);
		
		//max(12, 8);
		max(12, 7);
		
	}
	
	public static void exchange(int[] arr, int i, int j) {
		int temp = arr[i];
		arr[i] = arr[j];
		arr[j] = temp;
	}
	
	public static void selectSort(int[] arr) {
		for(int i=0; i<arr.length-1; i++) {
			for(int j=i+1; j<arr.length; j++) {
				if(arr[j] < arr[i]) {
					exchange(arr, i, j);
				}
			}
		}
		
		System.out.println(Arrays.toString(arr));
	}
	

	public static void selectSort2(int[] arr) {
		for(int i=0; i<arr.length-1; i++) {
			int index = i;
			int min = arr[i];
			for(int j=i+1; j<arr.length; j++) {
				if(arr[j] < min) {
					index = j;
					min = arr[j];
				}
			}
			
			exchange(arr, i, index);
		}
		
		System.out.println(Arrays.toString(arr));
	}
	
	public static void bubbleSort(int[] arr) {
		for(int i=arr.length-1; i>0; i--) {
			for(int j=0; j<i; j++) {
				if(arr[j] > arr[j+1]) {
					exchange(arr, j, j+1);
				}
			}
		}
		
		System.out.println(Arrays.toString(arr));
	}
	
	public static void insertSort(int[] arr) {
		for(int i=1; i<arr.length; i++) {
			for(int j=i; j>0; j--) {
				if(arr[j] < arr[j-1]) {
					exchange(arr, j, j-1);
				}
			}
		}
		
		System.out.println(Arrays.toString(arr));
	}
	
	public static void threadSort(int[] arr) {
		Thread[] ts = new Thread[arr.length];
		for(int i=0; i<arr.length; i++) {
			ts[i] = new MyThread(arr[i]);
		}
		
		for(int i=0; i<arr.length; i++) {
			ts[i].start();
		}
	}
	
	static class MyThread extends Thread {
		int time;

		public MyThread(int time) {
			super();
			this.time = time;
		}
		
		@Override
		public void run() {
			try {
				Thread.sleep(time*100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println(time);
			
			short s = 3;
//			s = s + 4;
			s += 5;
			
		}
		
	}
	
	// 1 2 3 4
	// 1 2 3 4 5
	public static void find(int[] arr, int v) {
		int start = 0;
		int end = arr.length-1;
		while(start < end) {
			int index = (start + end)/2;
			if(arr[index] == v) {
				System.out.println(index);
				break;
			} else if(arr[index] < v) {
				start = index + 1;
			} else {
				end = index - 1;
			}
		}
		
		if(start > end) {
			System.out.println("no");
		}
	}
	
	// 12 8
	// 8 4
	public static int max(int m, int n) {
		
		/*if(m < 0 || n <0) {
			System.out.println("error");
			return -1;
		}*/
		
		if(n == 0) {
			System.out.println("---" + m);
			return m;
		}
		
		return max(n, m%n);
	}

}
